<?php

namespace App\Pockets;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;

class GridDataModel extends Model
{
    protected $data = NULL;
    protected $total = NULL;
    protected $pageName = 'page';
    protected $currentPage = null;
    protected $extra_btn = [];

    public function __construct($data = [], $total = NULL, $options = [])
    {
        $this->data = $data;
        $this->total = $total ?: count($this->data);
        //grid元素key
        $this->primaryKey = isset($options['keyName']) ? $options['keyName'] : $this->primaryKey;
        //分页数
        $this->perPage = isset($options['perPage']) ? $options['perPage'] : Request::input('per_page', $this->perPage);
        //当前页码
        $this->currentPage = isset($options['currentPage']) ? $options['currentPage'] : $this->currentPage;
        //页码参数名
        $this->pageName = isset($options['pageName']) ? $options['pageName'] : $this->pageName;
        //额外按钮（跳转到下一页并...）
        $this->extra_btn = isset($options['extra_btn']) ? $options['extra_btn'] : $this->extra_btn;
    }

    public function paginate()
    {
        $paginator = new ReLengthAwarePaginator(static::hydrate($this->data), $this->total, $this->perPage, $this->currentPage, ['pageName' => $this->pageName, 'extra_btn'=> $this->extra_btn]);
        $paginator::defaultView('admin.pagination');
        $paginator->setPath(url()->current());
        return $paginator;
    }
//    public function orderBy(){
//        return $this;
//    }
}
